﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace EasyUIMvc
{
    public class MenuExtension : ExtensionBase
    {
        
        private readonly MenuSettings _settings;
        private IList<MenuItem> _menus;
        
        public MenuExtension(MenuSettings settings)
        {
            _settings = settings;
        }
        public MenuExtension Bind(IList<MenuItem> menus)
        {
            _menus = menus;
            return this;
        }

        public override string GetScripts()
        {
            throw new NotImplementedException();
        }
        public override MvcHtmlString GetHtml()
        {
            var menu = GetTopTag("div", "easyui-menu", _settings);
            foreach (var item in _menus)
            {
                var div = new TagBuilder("div");
                if (item.Childs.Count > 0)
                {
                    var span = new TagBuilder("span");
                    span.SetInnerText(item.Text);
                    div.InnerHtml += span;
                    div.InnerHtml += GetSubItems(item);
                }
                else
                {
                    if (item.Id != null)
                        div.MergeAttribute("id", item.Id);
                    div.SetInnerText(item.Text);
                }
                menu.InnerHtml += div;
            }
            return MvcHtmlString.Create(menu.ToString());
        }

        private TagBuilder GetSubItems(MenuItem item){
            var tag = new TagBuilder("div");
            foreach (var subitem in item.Childs)
            {
                var div = new TagBuilder("div");
                if (subitem.Childs.Count > 0)
                {
                    var span = new TagBuilder("span");
                    span.SetInnerText(subitem.Text);
                    div.InnerHtml += span;
                    div.InnerHtml += GetSubItems(subitem);
                }
                else
                {
                    if (subitem.Id != null)
                        div.MergeAttribute("id", subitem.Id);
                    div.SetInnerText(subitem.Text);
                }
                tag.InnerHtml += div;
            }
            return tag;
        }
    }
    public class MenuSettings : SettingsBase
    {
        public string Style { get; set; }
    }

    public class MenuItem
    {
        public MenuItem()
        {
            Id = null;
            Text = string.Empty;
            DataOptions = null;
            Childs = new List<MenuItem>();
        }
        public MenuItem(string text)
            : this()
        {
            Text = text;
        }
        /// <summary>
        /// The id attribute of menu item.
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// The item text.
        /// </summary>
        public string Text { get; set; }
        /// <summary>
        /// iconCls : A CSS class to display a 16x16 icon on item left.
        /// href : Set page location while clicking the menu item.
        /// disabled : Defines if to disable the menu item.
        /// onclick : The function to be called while clicking the menu item.
        /// </summary>
        public string DataOptions { get; set; }
        public List<MenuItem> Childs { get; set; }
    }

}
